package model;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;

import physics.*;

public class SquareBumper implements IGizmo {
	
	private String name;
	private int L;
	private int x;
	private int y;
	private Color col;
	private LineSegment[] lines;
	private physics.Circle[] corners;
	private Vect reflection = null;
		
	public SquareBumper(int gridL) {
		L = gridL;
		col = new Color(0xFF0000);
	}
	
	
	public SquareBumper(int gridL, String s) {
		name = s;
		L = gridL;
		col = new Color(0xFF0000);
	}
	
	
	@Override
	public void setPosition(int xpos, int ypos) {
		x = xpos * L; //top left corner in pixels
		y = ypos * L; //top left corner in pixels
		lines = new LineSegment[4];
		lines[0] = new LineSegment(x, y, x+L, y);	//top
		lines[1] = new LineSegment(x, y, x, y+L);	//left
		lines[2] = new LineSegment(x+L, y, x+L, y+L);	//right
		lines[3] = new LineSegment(x, y+L, x+L, y+L);	//bottom
		
		corners = new physics.Circle[4];
		corners[0] = new physics.Circle(x, y, 0);
		corners[1] = new physics.Circle(x+L, y, 0);
		corners[2] = new physics.Circle(x+L, y+L, 0);
		corners[3] = new physics.Circle(x, y+L, 0);
	}
		
	@Override
	public Graphics paintShape(Graphics g) {
		g.setColor(col);
		g.fillRect(x, y, L, L); 
		return g;
	}

	@Override
	public int getX() {
		return x;
	}

	@Override
	public int getY() {
		return y;
	}

	@Override
	public String getType() {
		return "Square";
	}

	@Override
	public Rectangle getBounds() {
		return new Rectangle(x, y, L, L);
	}

	@Override
	public Color getColor() {
		return col;
	}

	@Override
	public void setColor(Color col) {
		this.col = col;
	}

	@Override
	public void rotateClockwise() {
		// Don't need to rotate squares		
	}

	@Override
	public void triggerEvent() {
		int c = col.getRGB() << 8;
		c = (c == 0xFF000000)? 0xFF : c;
		col = new Color(c);
	}

	@Override
	public void doLinkedAction() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addLinkedGizmo(IGizmo giz) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public ArrayList<IGizmo> getLinkedGizmos() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addLinkedKey(int key) {
		// TODO Auto-generated method stub
	}

	@Override
	public ArrayList<Integer> getLinkedKeys() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public double timeUntilCollision(Ball ball) {
		double time=Double.POSITIVE_INFINITY, temp=0;

		for (LineSegment line : lines) {
			temp = Geometry.timeUntilWallCollision(line, ball.getCircle(), ball.getV());
			if (temp < time) {
				time = temp;
				reflection = Geometry.reflectWall(line, ball.getV()); 
			}
		}

		for (physics.Circle corner : corners) {
			temp = Geometry.timeUntilCircleCollision(corner, ball.getCircle(), ball.getV());
			if (temp < time) {
				time = temp;
				reflection = Geometry.reflectCircle(corner.getCenter(), ball.getCircle().getCenter(), ball.getV());
			}
		}
		return time;
	}

	@Override
	public Vect reflectionVector(Ball ball) {
		return reflection;
	}


	@Override
	public String getName() {
		return name;
	}


	@Override
	public int getTriangleRotations() {
		// TODO Auto-generated method stub
		return 0;
	}

}
